const mongoose = require('mongoose');
const Socket = require('./socket.js');
const path = require('path');
const {extendClassMethods} = require('./util.js');
const helper=require('think-helper');

mongoose.Promise = global.Promise;
/* 分页插件 */
mongoose.plugin(require('./helper/mongoose-paginate'));
mongoose.plugin(require('./helper/mongoose-helper'));


const MODELS = Symbol('think-mongoose-models');
const models = {};

class Mongoose {
    /**
     *
     * @param {String} modelName
     * @param {Object} config
     * @param {String} name
     */
    constructor(modelName, config, name) {
        this.modelName = modelName;
        this.config = config;
        if (models[name]) return models[name];

        const socket = Socket.getInstance(this.config);
        const connection = socket.createConnection();


        let schema = this.schema || {};
        if (!(schema instanceof mongoose.Schema)) {
            schema = new mongoose.Schema(schema);
        }
        const model = connection.model(this.tableName, schema, config.useCollectionPlural ? null : this.tableName);
        this.modelClass = class extends model {
        };
        extendClassMethods(this.modelClass, this);
        models[name] = this.modelClass;
        return this.modelClass;
    }

    /**
     * get table prefix
     */
    get tablePrefix() {
        return this.config.prefix || '';
    }

    /**
     * get table name, with table prefix
     */
    get tableName() {
        return this.tablePrefix + this.modelName;
    }

    /**
     * get all store models
     */
    get models() {
        return this[MODELS] || {};
    }

    /**
     * set models
     */
    set models(value) {
        this[MODELS] = value;
    }

    /**
     * get model instance
     * @param {String} name
     */
    mongoose(name) {
        const ModelClass = this.models[name] || Mongoose;
        // let ModelClass = this.models[name];
        // if (helper.isEmpty(ModelClass)) {
        //     console.log("指定的模型不存在");
        //     ModelClass = Mongoose;
        // }

        const modelName = path.basename(name);
        // console.log(name);
        const instance = new ModelClass(modelName, this.config, name);
        instance.models = this.models;
        return instance;
    }
}


Mongoose.mongoose = mongoose;
Mongoose.Schema = mongoose.Schema;
Mongoose.Mixed = mongoose.Schema.Types.Mixed;
Mongoose.ObjectId = mongoose.Schema.Types.ObjectId;

module.exports = Mongoose;
